<?php
namespace app\controller\api;


use app\exception\ErrnoPlus;
use app\service\DescribeInfoService;
use app\service\TutorialVideoService;
use app\validate\TutorialVideoValidate;
use share\controller\ApiBaseController;
use share\utils\Helper;

class TutorialVideoController extends ApiBaseController{

    protected $beforeActionList = [
        "checkSign",
        "checkUser", 
    ];

    /**
     * @description: 
     * @return {*}
     * @use: 
     */
    public function initValidate(){
        $this->validate = new TutorialVideoValidate();
    }

    /**
     * 获取所有module
     */
    public function list_module_action(){
        $input = $this->check();     
        $res = DescribeInfoService::getAllModuleInfoByLanguage($input);    
        if($res){
            $data = [];
            foreach($res as $item){
                $data[] = [
                    'id' => $item['id'],
                    'describeName' => $item['describe_name'],
                ];
            }
            return self::ok($data);
        }  
        return self::fail("no data",ErrnoPlus::NOT_BACK_DATA);
    }

    /**
     * 获取指定module下的教程
     */
    public function module_info_action(){
        $input = $this->check();
        $userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
        $input['system'] = Helper::getClientTypeByUserAgent($userAgent);
        $res = TutorialVideoService::getAllByModule($input);     
        if(empty($res)){
            return self::fail("no data",ErrnoPlus::NOT_BACK_DATA);
        } 
        return self::ok($res);
    }

    /**
     * 搜索视频教程
     */
    public function search_action(){
        $input = $this->check();
        $input['system'] = Helper::getClientTypeByUserAgent($_SERVER['HTTP_USER_AGENT']);
        $res = TutorialVideoService::searchByTitle($input);    
        if(empty($res)){
            return self::fail("no data",ErrnoPlus::NOT_BACK_DATA);
        }   
        return self::ok($res);
    }

}